<?php
declare (strict_types = 1);

namespace app\middleware;
use think\exception\ValidateException;


class TestCheck
{
    use \liliuwei\think\Jump;
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        // 前置中间件行为,更适合做验证
        // dump('这是前置中间件行为打印的文件！');
        $scene = app('request')->action();
        if ($scene == 'dologin') {
            # code...
            $data = [
                'name'      =>      $request->post('unickemailtel','','trim,htmlspecialchars'),
                'pwd'      =>      $request->post('upa','','trim'),
                'code'      =>      $request->post('ucode','','trim')
            ];
        }else {
            # code...
            $data = [
                'code'    =>      $request->post('ucode','','trim'),
                'name'   =>      $request->post('unick','','trim,htmlspecialchars'),
                'pwd'     =>      $request->post('upa','','trim'),
                'pwd2'    =>      $request->post('upa2','','trim'),
                'email'   =>      $request->post('uemail','','trim'),
                'tel'     =>      $request->post('utel','','trim')
            ];
        }
        try {
            validate(get_class(app('fcheck')))
                // -> scene('midcheck')
                ->batch(true)
                ->scene($scene)
                ->check($data);
        } catch (ValidateException $e) {
            // $this -> error($e ->getError(),'index/view');
            // $this -> error('发生错误!' .$e->getError());
            $this -> error(implode($e->getError()));
        }
        $u =  $next($request);
        return $u;
        // 后置中间件
        // dump('这是后置中间件行为打印的文件！');
        // return $r;
        //中间件必须返回Response对象
    }
    /*
    * 结束调度
    */
    public function end(\think\Response $response)
    {
        // 执行的时机适合于写入方法
        // dump('这是结束调度方法打印的内容');
    }

}